package mi.payment.ui.admin;

import android.os.Bundle;
import android.os.RemoteException;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.ui.print.helper.BasePrintController;
import mi.payment.ui.print.helper.PrintController;
import mi.payment.ui.base.ParentActivity;
import mi.payment.ui.print.BasePrintCallback;
import mi.payment.utils.VersionUtil;
import mi.payment.utils.print.DataPrinterKernel;
import sunmi.sunmiui.title.Title;
import woyou.aidlservice.jiuiv5.ICallback;
import woyou.aidlservice.jiuiv5.IWoyouService;

/**
 * 参数打印
 * Created by lj on 2017/2/15.
 */
public class ParamsPrintActivity extends ParentActivity {
    private View merchantInfoView, transactionSettingInfoView, systemSettingsInfoView, communicationParamsInfoView, versionInfoView, restInfoView, printAll;
    private DataPrinterKernel mDatePrinterKernerl;
    private String packageName, version;
    private int what = -1;
    private BasePrintController printController;
    private IWoyouService printService;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_params_print);
        printController = new PrintController(this);
        mDatePrinterKernerl = DataPrinterKernel.INSTANCE();
        mDatePrinterKernerl.setCpVersion(printController.isPrintCpVersion());
        mDatePrinterKernerl.setSmallTicketTail(printController.getSmallTicketTail());
        getVersionInfo();
        initView();
        setClickListener();
    }

    private void initView() {
        printService = DataPrinterKernel.INSTANCE().getPrinterService();
        Title mTitle;
        TextView leftText, rightText;
        ImageView enterImage;
        mTitle = (Title) findViewById(R.id.params_print_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.parameters_print));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ParamsPrintActivity.this.finish();
            }
        }, null, null, null);

        merchantInfoView = findViewById(R.id.merchant_info);
        transactionSettingInfoView = findViewById(R.id.transaction_setting_info);
        systemSettingsInfoView = findViewById(R.id.system_settings_info);
        communicationParamsInfoView = findViewById(R.id.communication_params_info);
        versionInfoView = findViewById(R.id.version_info);
        restInfoView = findViewById(R.id.rest_info);
        printAll = findViewById(R.id.print_all);
        leftText = (TextView) merchantInfoView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.merchant_info));

        leftText = (TextView) transactionSettingInfoView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.transaction_setting));

        leftText = (TextView) systemSettingsInfoView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.system_settings_info));

        leftText = (TextView) communicationParamsInfoView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.communication_params_info));

        leftText = (TextView) versionInfoView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.version_info));

        leftText = (TextView) restInfoView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.rest_info));

        leftText = (TextView) printAll.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.print_all));


        enterImage = (ImageView) merchantInfoView.findViewById(R.id.enter_img);
        enterImage.setVisibility(View.GONE);

        enterImage = (ImageView) transactionSettingInfoView.findViewById(R.id.enter_img);
        enterImage.setVisibility(View.GONE);

        enterImage = (ImageView) systemSettingsInfoView.findViewById(R.id.enter_img);
        enterImage.setVisibility(View.GONE);

        enterImage = (ImageView) communicationParamsInfoView.findViewById(R.id.enter_img);
        enterImage.setVisibility(View.GONE);

        enterImage = (ImageView) versionInfoView.findViewById(R.id.enter_img);
        enterImage.setVisibility(View.GONE);

        enterImage = (ImageView) restInfoView.findViewById(R.id.enter_img);
        enterImage.setVisibility(View.GONE);

        enterImage = (ImageView) printAll.findViewById(R.id.enter_img);
        enterImage.setVisibility(View.GONE);


        rightText = (TextView) merchantInfoView.findViewById(R.id.item_right_text);
        rightText.setText(getString(R.string.click_to_print));

        rightText = (TextView) transactionSettingInfoView.findViewById(R.id.item_right_text);
        rightText.setText(getString(R.string.click_to_print));

        rightText = (TextView) systemSettingsInfoView.findViewById(R.id.item_right_text);
        rightText.setText(getString(R.string.click_to_print));

        rightText = (TextView) communicationParamsInfoView.findViewById(R.id.item_right_text);
        rightText.setText(getString(R.string.click_to_print));

        rightText = (TextView) versionInfoView.findViewById(R.id.item_right_text);
        rightText.setText(getString(R.string.click_to_print));

        rightText = (TextView) restInfoView.findViewById(R.id.item_right_text);
        rightText.setText(getString(R.string.click_to_print));

        rightText = (TextView) printAll.findViewById(R.id.item_right_text);
        rightText.setText(getString(R.string.click_to_print));
    }

    private void setClickListener() {
        merchantInfoView.setOnClickListener(this);
        transactionSettingInfoView.setOnClickListener(this);
        systemSettingsInfoView.setOnClickListener(this);
        communicationParamsInfoView.setOnClickListener(this);
        versionInfoView.setOnClickListener(this);
        restInfoView.setOnClickListener(this);
        printAll.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        try {
            int printStatus = printService.updatePrinterState();
            if (printStatus != 1) {
                showToast(getString(R.string.printer_error));
                return;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
            showToast(getString(R.string.printer_error));
            return;
        }
        switch (v.getId()) {
            case R.id.merchant_info:
                //商户信息打印
                printMerchantInfo(mICallBack);
                break;
            case R.id.transaction_setting_info:
                //交易控制打印
                printTransactionInfo(mICallBack);
                break;
            case R.id.system_settings_info:
                //系统控制打印
                printSystemInfo(mICallBack);
                break;
            case R.id.communication_params_info:
                //通讯参数打印
                printCommunicationInfo(mICallBack);
                break;
            case R.id.version_info:
                //版本信息打印
                printVersionInfo(mICallBack);
                break;
            case R.id.rest_info:
                //其他信息打印
                printOtherInfo(mICallBack);
                break;
            case R.id.print_all:
                printMerchantInfo(printAllCallback);
                break;
        }
    }

    /**
     * 获取应用信息
     */
    private void getVersionInfo() {
        packageName = VersionUtil.getInstance().getPackageName(this);
        version = VersionUtil.getInstance().getVersionName(this);
    }

    /**
     * 打印商户参数
     */
    private void printMerchantInfo(ICallback iCallback) {
        what = 1;
        mDatePrinterKernerl.printMerchantInfo(packageName, version, iCallback);
    }

    private void printOtherInfo(ICallback iCallback) {
        what = 6;
        mDatePrinterKernerl.printOtherInfo(iCallback);
    }

    private void printTransactionInfo(ICallback iCallback) {
        what = 2;
        mDatePrinterKernerl.printTransactionInfo(iCallback);
    }

    private void printSystemInfo(ICallback iCallback) {
        what = 3;
        mDatePrinterKernerl.printSystemParams(iCallback);
    }

    private void printCommunicationInfo(ICallback iCallback) {
        what = 4;
        mDatePrinterKernerl.printCommunicationInfo(iCallback);
    }

    private void printVersionInfo(ICallback iCallback) {
        what = 5;
        mDatePrinterKernerl.printVersionInfo(version, iCallback);
    }

    private ICallback mICallBack = new BasePrintCallback() {
        @Override
        public void onPrintResult(int code, String msg) throws RemoteException {
            if (code == 1) {
                switch (what) {
                    case 1:
                        mDatePrinterKernerl.printMerchantInfo(packageName, version, mICallBack);
                        break;
                    case 2:
                        mDatePrinterKernerl.printOtherInfo(mICallBack);
                        break;
                    case 3:
                        mDatePrinterKernerl.printTransactionInfo(mICallBack);
                        break;
                    case 4:
                        mDatePrinterKernerl.printSystemParams(mICallBack);
                        break;
                    case 5:
                        mDatePrinterKernerl.printCommunicationInfo(mICallBack);
                        break;
                    case 6:
                        mDatePrinterKernerl.printVersionInfo(version, mICallBack);
                        break;
                }
            }
        }
    };

    private ICallback printAllCallback = new BasePrintCallback() {
        @Override
        public void onPrintResult(int code, String msg) throws RemoteException {
            if (code == 0) {
                try {
                    int printStatus = printService.updatePrinterState();
                    if(printStatus != 1){
                        showToast(getString(R.string.printer_error));
                        return;
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                    showToast(getString(R.string.printer_error));
                    return;
                }
                switch (what) {
                    case 1:
                        printTransactionInfo(printAllCallback);
                        break;
                    case 2:
                        printSystemInfo(printAllCallback);
                        break;
                    case 3:
                        printCommunicationInfo(printAllCallback);
                        break;
                    case 4:
                        printVersionInfo(printAllCallback);
                        break;
                    case 5:
                        printOtherInfo(printAllCallback);
                        break;
                    case 6:
                        break;
                }
            }
        }
    };
}
